package com.dy.security.controller;

import com.dy.security.dto.UserDTO;
import com.dy.security.entity.UserEntity;
import com.dy.security.mapper.UserMapper;
import com.dy.security.service.UserService;
import com.dy.sucetity.utils.ApiResousUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ding-yu
 * @Date: 2022/5/24 9:39
 * @Desctiption: (描述) 三种密码加密策略可以同时使用
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserService userService;


    /**
     * 注册
     * @return String
     */
    @ApiOperation("注册(Security默认加密)")
    @PostMapping("/register")
    public ApiResousUtil<Boolean> register(@RequestBody UserDTO user){
        UserEntity userEntity = new UserEntity();
        userEntity.setRoles("ADMIN")
            .setEnable(true)
                .setUserName(user.getUserName())
                .setPassword(passwordEncoder.encode(user.getPassword()));
        return ApiResousUtil.ok(userService.save(userEntity));
    }

    /**
     * 注册
     * @return String
     */
    @ApiOperation("注册(MD5加密)")
    @PostMapping("/registerMd5")
    public ApiResousUtil<Boolean> registerMd5(@RequestBody UserDTO user){
        UserEntity userEntity = new UserEntity();
        userEntity.setRoles("ADMIN")
                .setEnable(true)
                .setUserName(user.getUserName());

        Map<String,PasswordEncoder> encoders = new HashMap<>(16);
        encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
        DelegatingPasswordEncoder md5Encode = new DelegatingPasswordEncoder("MD5", encoders);
        //对密码进行加密
        userEntity.setPassword(md5Encode.encode(user.getPassword()));

        return ApiResousUtil.ok(userService.save(userEntity));
    }
    /**
     * 注册
     * @return String
     */
    @ApiOperation("注册(不加密)")
    @PostMapping("/registerNoop")
    public ApiResousUtil<Boolean> registerNoop(@RequestBody UserDTO user){
        UserEntity userEntity = new UserEntity();
        userEntity.setRoles("ADMIN")
                .setEnable(true)
                .setUserName(user.getUserName());

        Map<String,PasswordEncoder> encoders = new HashMap<>(16);
        encoders.put("noop", NoOpPasswordEncoder.getInstance());
        DelegatingPasswordEncoder noopEncode = new DelegatingPasswordEncoder("noop", encoders);

        //对密码进行加密
        userEntity.setPassword(noopEncode.encode(user.getPassword()));
        return ApiResousUtil.ok(userService.save(userEntity));
    }
}
